package com.dp.mallchat.common.chat.service.strategy;

import com.dp.mallchat.common.chat.dao.MessageDao;
import com.dp.mallchat.common.chat.domain.entity.Message;
import com.dp.mallchat.common.chat.domain.enums.MessageTypeEnum;
import com.dp.mallchat.common.chat.domain.vo.req.msg.FileMsgDTO;
import com.dp.mallchat.common.chat.domain.vo.req.msg.MessageExtra;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @Author: dupeng
 * @CreateTime: 2024-07-18  09:01
 * @Description: 文件消息处理器
 */
@Service
@Slf4j
public class FileMsgHandler extends AbstractMsgHandler<FileMsgDTO> {

    @Autowired
    private MessageDao messageDao;

    @Override
    MessageTypeEnum getMsgType() {
        return MessageTypeEnum.FILE;
    }

    @Override
    protected void saveMsg(Message message, FileMsgDTO body) {
        MessageExtra extra = Optional.ofNullable(message.getExtra()).orElse(new MessageExtra());
        Message update = new Message();
        update.setId(message.getId());
        extra.setFileMsg(body);
        update.setExtra(extra);
        messageDao.updateById(update);
    }

    @Override
    public Object showMsg(Message msg) {
        return msg.getExtra().getFileMsg();
    }

    @Override
    public Object showReplyMsg(Message msg) {
        return "文件：" + msg.getExtra().getFileMsg().getFileName();
    }

    @Override
    public String showContactMsg(Message msg) {
        return "[文件]" + msg.getExtra().getFileMsg().getFileName();
    }
}
